package com.clj.blesample.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.clj.blesample.R;

public class InputView extends FrameLayout {
    private TextView tvTitle;
    private EditText etContent;

    public InputView(@NonNull Context context) {
        this(context, null);
    }

    public InputView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View inputView = LayoutInflater.from(context).inflate(R.layout.input_view, this, false);
        tvTitle = inputView.findViewById(R.id.tvTitle);
        etContent = inputView.findViewById(R.id.etContent);
        addView(inputView);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputView);
        String title = ta.getString(R.styleable.InputView_title);
        boolean canEdit = ta.getBoolean(R.styleable.InputView_canEdit, true);
        tvTitle.setText(title);
        etContent.setEnabled(canEdit);
        if (!canEdit) {
            etContent.setHint("");
        }
        ta.recycle();
    }

    public String getContent() {
        return etContent.getText().toString();
    }

    public EditText getEtContent() {
        return etContent;
    }

    public void setContent(String content) {
        etContent.setText(content);
        etContent.setSelection(etContent.getText().length());
    }
}
